import json

from django.shortcuts import render,HttpResponse,redirect
from app_user import models
from app_user.models import UserInfo,Order,Goods

from app_user.utls.pagenation import Pagenation
from app_user.utls.form import user_address_add_ModeForm, edit_user_inforModeForm,edit_goods_ModeForm



def management(request):
    """后台管理界面"""


    return render(request, "management.html")



def management_user(request):
    """用户管理"""


    user_data = UserInfo.objects.filter(level=1).all()

    page_data = Pagenation(request, user_data)

    data = {
        "user_data": page_data.page_data,
        "page_string":page_data.page_string
    }

    return render(request, 'management_user.html', context=data)


def edit_account(request,id):
    """编辑账号"""
    user_data = UserInfo.objects.filter(id=id).first()

    if request.method == "GET":
        form = edit_user_inforModeForm(instance=user_data)
        data = {
            "form": form,
        }
        return render(request, "account_info_edit.html", context=data)

    form = edit_user_inforModeForm(data=request.POST, instance=user_data)
    if form.is_valid():
        form.save()
        return redirect("/management/user/")
    else:
        data = {
            "form": form,
        }
        return render(request, "account_info_edit.html", context=data)



def delete_account(request):
    """删除账号"""
    account_id = request.GET.get('uid')
    models.UserInfo.objects.filter(id=account_id).delete()

    data = {
        "status": True,
    }

    return HttpResponse(json.dumps(data))



def management_goods(request):
    """商品管理"""

    goods_data = Goods.objects.all()

    page_data = Pagenation(request, goods_data)

    data = {
        "goods_data": page_data.page_data,
        "page_string":page_data.page_string
    }

    return render(request, 'management_goods.html', context=data)

def management_goods_delete(request):
    """删除商品"""
    goods_id = request.GET.get('uid')
    models.Goods.objects.filter(id=goods_id).delete()
    data = {
        "status": True,
    }
    return HttpResponse(json.dumps(data))

def management_goods_up(request):
    """商品上架"""
    goods_id = request.GET.get('uid')
    models.Goods.objects.filter(id=goods_id).update(status=1)
    data = {
        "status": True,
    }
    return HttpResponse(json.dumps(data))


def management_goods_down(request):
    """商品下架"""
    goods_id = request.GET.get('uid')
    models.Goods.objects.filter(id=goods_id).update(status=2)
    data = {
        "status": True,
    }
    return HttpResponse(json.dumps(data))

def management_goods_edit(request,id):
    """编辑商品"""
    goods_data = Goods.objects.filter(id=id).first()
    if request.method == "GET":
        form = edit_goods_ModeForm(instance=goods_data)
        data = {
            "form": form,
        }
        return render(request, "goods_info_edit.html", context=data)

    form = edit_goods_ModeForm(data=request.POST, instance=goods_data)
    if form.is_valid():
        form.save()
        return redirect("/management/goods/")
    else:
        data = {
            "form": form,
        }
        return render(request, "goods_info_edit.html", context=data)




def get_Category_data(data_obj):
    """获取分类销售数据"""
    for data in data_obj:
        goods_obj = Goods.objects.filter(id=data.id).first()
        if goods_obj.category == 1:
            pass
        elif goods_obj.category == 2:
            pass
        elif goods_obj.category == 3:
           pass
        elif goods_obj.category == 4:
            pass
        elif goods_obj.category == 5:
           pass
        elif goods_obj.category == 6:
            pass




def management_sales(request):
    """销售管理"""
    day_sales = Order.objects.filter(order_status=2).all()


    return render(request, 'management_sales.html')



def management_sales_get_data(request):
    """动态获取sales数据"""
    data ={
        "categories": ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
        "values": [5, 20, 36, 10, 10, 20]
    }
    print(data)
    return HttpResponse(json.dumps(data))



def management_sales_goods(request):
    goods_data = Goods.objects.all()

    page_data = Pagenation(request, goods_data)

    data = {
        "goods_data": page_data.page_data,
        "page_string":page_data.page_string
    }

    return render(request, 'management_sales_goods.html', context=data)

def management_default_recommendation(request):
    """默认推荐"""
    goods_id = request.GET.get('uid')
    models.Goods.objects.filter(id=goods_id).update(recommendation=True)
    models.new_product.objects.create(goods_id=goods_id)
    models.goods_slide_show.objects.create(goods_id=goods_id)
    data = {
        "status": True,
    }
    return HttpResponse(json.dumps(data))

def management_delete_recommendation(request):
    """删除推荐"""
    goods_id = request.GET.get('uid')
    models.Goods.objects.filter(id=goods_id).update(recommendation=False)
    models.new_product.objects.filter(goods_id=goods_id).delete()
    models.goods_slide_show.objects.filter(goods_id=goods_id).delete()
    data = {
        "status": True,
    }
    return HttpResponse(json.dumps(data))